<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>微博第三方登录 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/myhugo/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0d ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/myhugo/"><h1>我的博客</h1></a>
      <p class="lead">
       小白的逆袭之旅 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/myhugo/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>微博第三方登录</h1>
  <time datetime=2020-04-28T20:49:14&#43;0800 class="post-date">Tue, Apr 28, 2020</time>
  <h3 id="第三方登录">第三方登录</h3>
<p>具体流程
<a href="https://blog.csdn.net/qq_42249896/article/details/87433349">https://blog.csdn.net/qq_42249896/article/details/87433349</a></p>
<h3 id="思维脑洞">思维脑洞</h3>
<p><img src="/static/siwei.png" alt="第三方登录流程图"></p>
<h3 id="vue登录页面请求微博登录接口">vue登录页面请求微博登录接口</h3>
<pre><code>&lt;template&gt;
    &lt;div&gt;
        &lt;button @click='sina'&gt;微博登陆&lt;/button&gt;
    &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
export default {
    methods:{
        sina:function() { 
            //跳转 
            //微博api：https://api.weibo.com/oauth2/authorize
            //client_id：微博开放平台的appid
            //redirect_uri：返回的跳转地址
            window.location.href =&quot;https://api.weibo.com/oauth2/authorize?client_id=1721358960&amp;redirect_uri=http://127.0.0.1:8000/md_admin/weibo&quot;;
        }
    }
}
&lt;/script&gt;
</code></pre><h3 id="django路由">django路由</h3>
<pre><code>from myapp.md_user import wb_back
urlpatterns = [
    path('md_admin/weibo/',wb_back),
]
</code></pre><h3 id="django接口">django接口</h3>
<pre><code>#新浪微博回调
def wb_back(request):
    #接受参数
    code = request.GET.get('code',None)
    #定义token接口地址
    url = &quot;https://api.weibo.com/oauth2/access_token&quot;
    #定义参数
    re=requests.post(url,data={
        'client_id':'1721358960',
        'client_secret':'6c3aae7af5600f10b9a0b40b6a952d01',
        &quot;grant_type&quot;:&quot;authorization_code&quot;,
        'code':code,
        &quot;redirect_uri&quot;:&quot;http://127.0.0.1:8000/md_admin/weibo&quot;
    })
    print(re.json())

    #换取新浪用户昵称
    res = requests.get('https://api.weibo.com/2/users/show.json',
                       params={'access_token': re.json()['access_token'], 'uid': re.json()['uid']})
    sina_id=''
    user_id=''
    #判断是否登陆过
    user=User.objects.filter(username=str(res.json()['name'])).first()
    if user:
        #代码曾经用该账号登陆过
        sina_id=user.username
        user_id=user.id
    else:
        user=User(username=str(res.json()['name']),password='')
        user.save()
        user=User.objects.filter(username=str(res.json()['name'])).first()
        sina_id=user.username
        user_id=user.id
    print(sina_id,user_id)

    return redirect('http://127.0.0.1:8080?sina_id=' + str(sina_id) + &quot;&amp;uid=&quot; + str(user_id))
</code></pre><h3 id="vue第三方登录的接入">vue第三方登录的接入</h3>
<pre><code>&lt;script&gt;
export default {
  mounted:function(){
        //接受第三方参数
        var sina_id = this.$route.query.sina_id;
        var user_id = this.$route.query.uid;
        //判断是否有第三方登陆
        if(sina_id){
            //存入localStorage
            localStorage.setItem('username',sina_id)
            localStorage.setItem('uid',user_id)
        }
    }
}
&lt;/script&gt;
</code></pre><h3 id="登出操作">登出操作</h3>
<pre><code>methods: {
		//登出
		logout(){
			//删除localstorage
			localStorage.removeItem('username')
			//跳转
			this.username = ''
			this.$router.push('/')
		}
	},
</code></pre>
</div>


    </main>

    
  </body>
</html>
